package top.frsf.dashboard.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.frsf.dashboard.pojo.dto.LoginRequestDTO;
import top.frsf.dashboard.pojo.vo.Result;
import top.frsf.dashboard.properties.AuthProperties;
import top.frsf.dashboard.utils.JwtUtil;

/**
 * 认证接口
 *
 * @author He Kuntao
 * @since 2025/6/3 11:46
 */
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class AuthController {

    private final JwtUtil jwtUtil;

    private final AuthProperties authProperties;

    @PostMapping("/login")
    public Result<String> login(@Validated @RequestBody LoginRequestDTO req) {
        if (!authProperties.getUsername().equals(req.getUsername()) || !authProperties.getPassword().equals(req.getPassword())) {
            return Result.error(401, "用户名或密码错误");
        }

        String token = jwtUtil.generateToken(req.getUsername());
        return Result.success(token);
    }

}
